package com.huawei.fastengine.fastview.download.utils;

import com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;

public class RefectUtils {
    private static final String LOG_TAG = "RefectUtils";

    public static boolean getBooleanFiled(String str, String str2, boolean z) {
        Class cls = null;
        try {
            cls = Class.forName(str);
        } catch (Exception e) {
            logException(e, LOG_TAG);
        }
        if (cls != null) {
            try {
                AccessibleObject declaredField = cls.getDeclaredField(str2);
                setAccessible(declaredField, true);
                z = declaredField.getBoolean(null);
            } catch (Exception e2) {
                logException(e2, LOG_TAG);
            } catch (Exception e22) {
                logException(e22, LOG_TAG);
            } catch (Exception e222) {
                logException(e222, LOG_TAG);
            }
        }
        return z;
    }

    public static int getIntFiled(String str, String str2, int i) {
        try {
            Class cls = Class.forName(str);
            AccessibleObject declaredField = cls.getDeclaredField(str2);
            setAccessible(declaredField, true);
            i = declaredField.getInt(cls);
        } catch (Exception e) {
            logException(e, LOG_TAG);
        } catch (Exception e2) {
            logException(e2, LOG_TAG);
        } catch (Exception e22) {
            logException(e22, LOG_TAG);
        } catch (Exception e222) {
            logException(e222, LOG_TAG);
        } catch (Exception e2222) {
            logException(e2222, LOG_TAG);
        }
        return i;
    }

    public static Object invokeFun(Class<?> cls, Object obj, String str, Class<?>[] clsArr, Object[] objArr) {
        Method method;
        Object invoke;
        Method method2 = null;
        try {
            method = cls.getMethod(str, clsArr);
        } catch (Exception e) {
            logException(e, LOG_TAG);
            method = method2;
        }
        if (method != null) {
            try {
                invoke = method.invoke(obj, objArr);
            } catch (Exception e2) {
                logException(e2, LOG_TAG);
            } catch (Exception e22) {
                logException(e22, LOG_TAG);
            } catch (Exception e222) {
                logException(e222, LOG_TAG);
            }
        }
        return invoke;
    }

    public static Object invokeFun(String str, String str2, Class<?>[] clsArr, Object[] objArr) {
        Class cls = null;
        if (clsArr == null || objArr == null || clsArr.length != objArr.length) {
            FastViewLogUtils.d(LOG_TAG, "invokeFun params invalid");
            return cls;
        }
        Class cls2;
        try {
            cls2 = Class.forName(str);
        } catch (Exception e) {
            logException(e, LOG_TAG);
            cls2 = cls;
        }
        if (cls2 == null) {
            return cls;
        }
        Object newInstance;
        try {
            newInstance = cls2.newInstance();
        } catch (Exception e2) {
            logException(e2, LOG_TAG);
        } catch (Exception e22) {
            logException(e22, LOG_TAG);
        }
        return invokeFun(cls2, newInstance, str2, clsArr, objArr);
    }

    public static Object invokeStaticFun(String str, String str2, Class<?>[] clsArr, Object[] objArr) {
        Object invoke;
        Class cls = null;
        if (clsArr == null || objArr == null || clsArr.length != objArr.length) {
            FastViewLogUtils.e(LOG_TAG, "invokeStaticFun 参数有误");
        } else {
            Class cls2;
            try {
                cls2 = Class.forName(str);
            } catch (Exception e) {
                logException(e, LOG_TAG);
                cls2 = cls;
            }
            if (cls2 != null) {
                Method declaredMethod;
                try {
                    declaredMethod = cls2.getDeclaredMethod(str2, clsArr);
                } catch (Exception e2) {
                    logException(e2, LOG_TAG);
                    Object obj = cls;
                }
                if (declaredMethod != null) {
                    try {
                        invoke = declaredMethod.invoke(cls2, objArr);
                    } catch (Exception e22) {
                        logException(e22, LOG_TAG);
                    } catch (Exception e222) {
                        logException(e222, LOG_TAG);
                    } catch (Exception e2222) {
                        logException(e2222, LOG_TAG);
                    }
                }
            }
        }
        return invoke;
    }

    private static void logException(Exception exception, String str) {
        if (exception != null) {
            FastViewLogUtils.e(str, "exception: " + exception.getClass().getName());
        }
    }

    public static void setAccessible(AccessibleObject accessibleObject, boolean z) {
        if (accessibleObject != null) {
            accessibleObject.setAccessible(z);
        }
    }
}
